package org.grow.sha.tool;



import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;


public class RobotEx extends Robot {
    private Clipboard clipboard;
    public RobotEx() throws AWTException {
        super();
        this.clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    }

    public RobotEx(GraphicsDevice screen) throws AWTException {
        super(screen);
        this.clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    }

    public void moveAndClick(int x, int y){
        super.mouseMove(x,y);
        super.delay(300);
        super.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        super.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        super.delay(1000);
    }
    public void moveAndClickQuick(int x, int y){
        super.mouseMove(x,y);
        super.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        super.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        super.delay(800);
    }
    public void click(){
        super.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        super.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        super.delay(1000);
    }
    public void click(int ms){
        super.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        super.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        super.delay(ms);
    }
    public void input(String content){
        clipboard.setContents(new StringSelection(content),null);
        super.keyPress(KeyEvent.VK_CONTROL);
        super.keyPress(KeyEvent.VK_V);
        super.keyRelease(KeyEvent.VK_V);
        super.keyRelease(KeyEvent.VK_CONTROL);
        super.delay(800);
    }
    public double distance(Color c1,Color c2){
        float[] f1 = c1.getRGBColorComponents(null);
        float[] f2 = c2.getRGBColorComponents(null);
        double dr= f1[0]-f2[0];
        double dg= f1[1]-f2[1];
        double db= f1[2]-f2[2];
        return Math.sqrt(dr*dr+dg*dg+db*db);

    }
    public boolean closeTo(Color c1,Color c2){
        double distance = distance(c1, c2);
        if (distance<0.1) return true;
        return false;
    }
//   each r g b +-5
    public boolean veryCloseTo(Color c1,Color c2){
        double distance = distance(c1, c2);
        if (distance<0.05) return true;
        return false;
    }
//  each  r g b +-2
    public boolean almostSameTo(Color c1,Color c2){
        double distance = distance(c1, c2);
        if (distance<0.015) return true;
        return false;
    }
    public void waitAppear(int x, int y, Color c){
        int timeout = 0;
        while (timeout<20){
            if (this.almostSameTo(super.getPixelColor(x,y),c)){
                break;
            }
            super.delay(1000);
            timeout++;
        }
    }
    public void waitThenClick(int x,int y ,Color c){

//        int timeout = 0;
//        while (timeout<20){
//            if (this.almostSameTo(super.getPixelColor(x,y),c)){
//                break;
//            }
//            super.delay(1000);
//            timeout++;
//        }
        this.waitAppear(x,y,c);
        this.moveAndClick(x,y);
    }
    public void keepClickUntil(int x,int y ,Color c){
        keepClickUntil(x,y,c,15);
    }
    public void keepClickUntil(int x,int y ,Color c,int maxTimes){
        int k = 0;
        while (k<maxTimes){
            this.moveAndClickQuick(x, y);
            if (this.almostSameTo(super.getPixelColor(x,y),c)){
                break;
            }
            k++;
        }
    }
    public void keepClickUntilTarget(int clickX,int clickY,int targetX,int targetY,Color targetColor){
        int k = 0;
        while (k<10){
            this.moveAndClickQuick(clickX, clickY);
            if (this.almostSameTo(super.getPixelColor(targetX,targetY),targetColor)){

                break;
            }
            k++;
        }
//        System.out.println("循环点击次数 : "+k);
    }
    public void keepClickUntilTargetNot(int clickX,int clickY,int targetX,int targetY,Color targetColor){
        int k = 0;
        while (k<10){
            this.moveAndClickQuick(clickX, clickY);
            if (!this.almostSameTo(super.getPixelColor(targetX,targetY),targetColor)){

                break;
            }
            k++;
        }
//        System.out.println("循环点击次数 : "+k);
    }
    public boolean createErrorSnapshot(File file) throws IOException {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        BufferedImage screenCapture = super.createScreenCapture(new Rectangle(screenSize));
        boolean png = ImageIO.write(screenCapture, "png", file);
        return png;
    }
    public boolean createDebugSnapShot(String title){
        long l = System.currentTimeMillis();
        String dir = System.getProperty("user.home")+"/sgs_debug";
        if (Files.notExists(Paths.get(dir))){
            try {
                Files.createDirectory(Paths.get(dir));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String name = dir+"/"+title+l+".png";
        File file = new File(name);
        try {
            return this.createErrorSnapshot(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
